package com.renan.business;
import cn.hutool.core.lang.Console;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

/**
 * @author 张亚林
 */
@Component
public class OrderTask {
    /**
     * 默认是fixedDelay 上一次执行完毕时间后执行下一轮
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void run() throws InterruptedException {
        Console.log(Thread.currentThread().getName() + "使用cron  {}开始" + (LocalDateTime.now()));
        Thread.sleep(6000);
        Console.log(Thread.currentThread().getName() + "=====>>>>>使用cron  {}" + (LocalDateTime.now()));
    }

    /**
     * fixedRate:上一次开始执行时间点之后5秒再执行
     */
    @Scheduled(fixedRate = 5000)
    public void run1() throws InterruptedException {
        Console.log(Thread.currentThread().getName() + "使用fixedRate  {}开始" + (LocalDateTime.now()));
        Thread.sleep(6000);
        Console.log(Thread.currentThread().getName() + "=====>>>>>使用fixedRate  {}" + (LocalDateTime.now()));
    }

    /**
     * fixedDelay:上一次执行完毕时间点之后5秒再执行
     */
    @Scheduled(fixedDelay = 5000)
    public void run2() throws InterruptedException {
        Console.log(Thread.currentThread().getName() + "使用fixedDelay  {}开始" + (LocalDateTime.now()));

        Thread.sleep(60 * 1000);
        Console.log(Thread.currentThread().getName() + "=====>>>>>使用fixedDelay  {}" + (LocalDateTime.now()));
    }

    /**
     * 第一次延迟2秒后执行，之后按fixedDelay的规则每5秒执行一次
     */
    @Scheduled(initialDelay = 2000, fixedDelay = 5000)
    public void run3() throws InterruptedException {
        Console.log(Thread.currentThread().getName() + "使用initialDelay  {}开始" + (LocalDateTime.now()));
        Thread.sleep(7000);
        Console.log(Thread.currentThread().getName() + "=====>>>>>使用initialDelay  {}" + (LocalDateTime.now()));
    }
}
